#include "project.h"

char form_IDdata[20];
char form_psdata[7];

int cgiMain(int argc, const char *argv[])
{
	// 创建消息队列
	key_t key = 0;

	if ((key = ftok("/home/linux", 'a')) == -1)
	{
		perror("ftok");
		return -1;
	}

	msqid = msgget(key, IPC_CREAT | 0666);
	if (msqid == -1)
	{
		perror("msgget");
		return -1;
	}

	memset(&msg, 0, sizeof(msg_t));

	/*获取网页用户名和密码*/
	cgiFormString("ID", form_IDdata, 7);	   // 获取网页form表单
	cgiFormString("PASSWORD", form_psdata, 7); // 获取网页form表单 密码

	/*用户名和密码拷贝到消息结构 登录字段*/
	strcpy(msg.log_msg.name, form_IDdata);
	strcpy(msg.log_msg.password, form_psdata);

	/*设置消息类型 1 登录*/
	msg.mtype = 1;
	/*设置登录标志 0*/
	msg.log_msg.flags = 0;

	/*发送消息置消息队列*/
	if (msgsnd(msqid, &msg, sizeof(msg_t) - sizeof(long), 0) == -1)
	{
		perror("msgsnd");
		return -1;
	}

	memset(&msg, 0, sizeof(msg_t));
	/*读取消息队列返回消息 类型5*/
	msgrcv(msqid, &msg, sizeof(msg_t) - sizeof(long), 5, 0);

	printf("Content-type: text/html;charset=\"UTF-8\"\n\n");
	printf("<html>\n");
	printf("<body>\n");

	if (1 == msg.log_msg.flags)
	{
		// 跳转到 仓库选择界面
		printf("<script>window.location.href = '/store_select.html';</script>\n");
		return 0;
	}
	else
	{
		printf("<center><h1><p>用户名或密码错误</p>\n");
		printf("<a href=\"../index.html\">返回</a>\n");
		printf("</h1></center>\n");
		return -1;
	}

	printf("</body>\n");
	printf("</html>\n");

	return 0;
}
